# import numpy as np
# import matplotlib.pyplot as plt
# import matplotlib as mpl
# from matplotlib.font_manager import FontProperties
import seaborn as sns

# # 设置全局字体配置
# plt.rcParams['font.size'] = 10
# plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'Arial Unicode MS']
# plt.rcParams['axes.unicode_minus'] = False
# plt.rcParams['text.usetex'] = False

# # 创建支持中文的字体属性
# chinese_font = FontProperties(fname=mpl.font_manager.findfont('SimHei'))

# 创建图形和子图（垂直排列）
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 8), dpi=100)

# ================== 图1：滑动窗口大小对隐私消耗的影响 ==================
# 滑动窗口大小
window_sizes = np.array([1, 3, 5, 7, 10, 15])

# 隐私消耗数据（单位：ε）
privacy_consumption = {
    'GP-AdaFL(ours)': np.array([5.8, 4.2, 3.8, 4.1, 4.5, 5.0]),
    'DP-FedAvg': np.array([6.0, 6.0, 6.0, 6.0, 6.0, 6.0]),
    'DP-FedANAW': np.array([5.5, 4.8, 4.5, 4.7, 5.2, 5.6])
}

# 绘制柱状图
bar_width = 0.25
x = np.arange(len(window_sizes))

# # 绘制GP-AdaFL柱状图
# bars1 = ax1.bar(x - bar_width, privacy_consumption['GP-AdaFL(ours)'], 
#                width=bar_width, color='#1f77b4', 
#                label='GP-AdaFL(ours)')

# # 绘制DP-FedAvg柱状图
# bars2 = ax1.bar(x, privacy_consumption['DP-FedAvg'], 
#                width=bar_width, color='#ff7f0e', 
#                label='DP-FedAvg')

# # 绘制DP-FedANAW柱状图
# bars3 = ax1.bar(x + bar_width, privacy_consumption['DP-FedANAW'], 
#                width=bar_width, color='#2ca02c', 
#                label='DP-FedANAW')

# # 添加数据标签
# def add_labels(bars):
#     for bar in bars:
#         height = bar.get_height()
#         ax1.annotate(f'{height:.1f}',
#                     xy=(bar.get_x() + bar.get_width() / 2, height),
#                     xytext=(0, 3),  # 3 points vertical offset
#                     textcoords="offset points",
#                     ha='center', va='bottom')

# add_labels(bars1)
# add_labels(bars2)
# add_labels(bars3)

# # 设置属性
# ax1.set_xlabel('滑动窗口大小', fontsize=12, fontproperties=chinese_font)
# ax1.set_ylabel('隐私消耗 (ε)', fontsize=12, fontproperties=chinese_font)
# ax1.set_title('图10：滑动窗口大小对隐私消耗的影响', fontsize=14, fontweight='bold', fontproperties=chinese_font)
# ax1.set_xticks(x)
# ax1.set_xticklabels(window_sizes)
# ax1.grid(True, linestyle='--', alpha=0.3, axis='y')
# ax1.legend(prop=chinese_font)

# # 标注最优窗口
# ax1.annotate('最优窗口大小 (5轮)\n隐私消耗最低 (ε=3.8)', 
#             xy=(2, 3.8), 
#             xytext=(3.5, 2.5),
#             arrowprops=dict(arrowstyle='->', color='dimgray'),
#             fontsize=10, fontproperties=chinese_font,
#             bbox=dict(boxstyle="round,pad=0.3", fc="white", ec="#1f77b4", alpha=0.8))

# ================== 图2：窗口大小与模型准确率的关系 ==================
# 准确率数据（%）
accuracy_data = {
    'GP-AdaFL(ours)': np.array([92.5, 95.2, 96.8, 96.5, 96.0, 95.2]),
    'DP-FedAvg': np.array([90.0, 90.0, 90.0, 90.0, 90.0, 90.0]),
    'DP-FedANAW': np.array([91.8, 94.5, 95.5, 95.2, 94.8, 94.0])
}

# 绘制折线图
ax2.plot(window_sizes, accuracy_data['GP-AdaFL(ours)'], 
        'o-', color='#1f77b4', linewidth=2, markersize=8,
        label='GP-AdaFL(ours)')

ax2.plot(window_sizes, accuracy_data['DP-FedAvg'], 
        's--', color='#ff7f0e', linewidth=2, markersize=8,
        label='DP-FedAvg')

ax2.plot(window_sizes, accuracy_data['DP-FedANAW'], 
        'D-.', color='#2ca02c', linewidth=2, markersize=8,
        label='DP-FedANAW')

# 设置属性
ax2.set_xlabel('滑动窗口大小', fontsize=12, fontproperties=chinese_font)
ax2.set_ylabel('模型准确率 (%)', fontsize=12, fontproperties=chinese_font)
# ax2.set_title('图11：滑动窗口大小对模型准确率的影响', fontsize=14, fontweight='bold', fontproperties=chinese_font)
ax2.set_ylim(85, 100)
ax2.grid(True, linestyle='--', alpha=0.7)
ax2.legend(prop=chinese_font)

# 标注最优窗口
# ax2.annotate('窗口大小=5时准确率最高 (96.8%)\n比基准方案提升6.8%', 
#             xy=(5, 96.8), 
#             xytext=(7, 94),
#             arrowprops=dict(arrowstyle='->', color='dimgray'),
#             fontsize=10, fontproperties=chinese_font,
#             bbox=dict(boxstyle="round,pad=0.3", fc="white", ec="#1f77b4", alpha=0.8))

# 添加技术标注
fig.text(0.5, 0.04, 
         "实验设置: 总隐私预算ε=5 | 数据集:MNIST | 模型:LeNet-5 | Non-IID参数α=0.5", 
         ha="center", fontsize=10, style='italic', fontproperties=chinese_font)

# 添加关键结论
# fig.text(0.5, 0.95, 
#          "关键结论: 滑动窗口大小=5轮时，隐私消耗最低(ε=3.8)且准确率最高(96.8%)，窗口过大或过小均导致性能下降",
#          ha="center", fontsize=12, fontproperties=chinese_font, 
#          bbox=dict(boxstyle="round,pad=0.3", fc="#f0f0f0", ec="black", alpha=0.8))

# 调整布局
plt.tight_layout(rect=[0, 0.05, 1, 0.95])
plt.subplots_adjust(hspace=0.25)

# 保存图像
plt.savefig('window_size_impact.png', bbox_inches='tight', dpi=300)
plt.show()
